#! /usr/bin/env python
#coding=utf-8
import wx
import config
import transform

class optionDialog(wx.Dialog):
    def __init__(self, parent):
        self.transformConfig = config.TransformConfig()
        self.fontConfig = config.FontConfig()

        wx.Dialog.__init__(self, parent, -1, u'选项')

        modeBox = wx.StaticBox(self, -1, u'模式')
        self.modeCh = wx.Choice(self, -1, choices = transform.trans_func.keys())
        self.Bind(wx.EVT_CHOICE, self.OnChMode, self.modeCh)
        font = wx.Font(family = wx.DEFAULT, face = self.fontConfig['face'], pointSize = self.fontConfig['size'], style = self.fontConfig['style'], weight = self.fontConfig['weight'])
        self.modeText = wx.TextCtrl(self, style = wx.TE_MULTILINE | wx.HSCROLL | wx.TE_READONLY, size=(300, 150))
        self.modeText.SetFont(font)
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.modeCh, 0, wx.TOP|wx.LEFT|wx.ALL, 5)
        sizer.Add(self.modeText, 0, wx.TOP|wx.LEFT|wx.ALL, 5)
        modeSizer = wx.StaticBoxSizer(modeBox, wx.VERTICAL)
        modeSizer.Add(sizer, 0, wx.TOP|wx.LEFT|wx.ALL, 10)
        
        fontQBox = wx.StaticBox(self, -1, u'字体修饰符')
        self.fontQCh = wx.Choice(self, -1, choices = transform.font_qualifier.keys())
        self.Bind(wx.EVT_CHOICE, self.OnChFontQ, self.fontQCh)
        self.fontQText = wx.TextCtrl(self, style = wx.TE_MULTILINE | wx.HSCROLL | wx.TE_READONLY, size=(300, 100))
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.fontQCh, 0, wx.TOP|wx.LEFT|wx.ALL, 5)
        sizer.Add(self.fontQText, 0, wx.TOP|wx.LEFT|wx.ALL, 5)
        fontQSizer = wx.StaticBoxSizer(fontQBox, wx.VERTICAL)
        fontQSizer.Add(sizer, 0, wx.TOP|wx.LEFT|wx.ALL, 10)
        
        typesetBox = wx.StaticBox(self, -1, u'排版')
        self.autoTypesetCb = wx.CheckBox(self, -1, u'自动分段')
        st = wx.StaticText(self, -1, u'每行的最大字数')
        self.lineLengthSpin = wx.SpinCtrl(self, -1, style = wx.SP_VERTICAL)
        self.lineLengthSpin.SetRange(1, 80)
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(st, flag = wx.RIGHT|wx.ALIGN_CENTER_VERTICAL, border = 5)
        sizer.Add(self.lineLengthSpin)
        typesetSizer = wx.StaticBoxSizer(typesetBox, wx.VERTICAL)
        typesetSizer.Add(self.autoTypesetCb, 0, wx.TOP|wx.LEFT|wx.ALL, 10)
        typesetSizer.Add(sizer, 0, wx.TOP|wx.LEFT|wx.ALL, 10)

        fontBox = wx.StaticBox(self, -1, u'字体')
        st = wx.StaticText(self, -1, u'编辑框内使用的字体')
        self.fontBt = wx.Button(self, 10, '%s %d' % (font.GetFaceName(), font.GetPointSize()))
        self.fontBt.SetFont(font)
        self.fontBt.SetSize(self.fontBt.GetBestSize())
        self.Bind(wx.EVT_BUTTON, self.OnSelectFont, self.fontBt)
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(st,  flag = wx.RIGHT|wx.ALIGN_CENTER_VERTICAL, border = 5)
        sizer.Add(self.fontBt)
        fontSizer = wx.StaticBoxSizer(fontBox, wx.VERTICAL)
        fontSizer.Add(sizer, 0, wx.TOP|wx.LEFT|wx.ALL, 10)
        
        buttonSizer = wx.StdDialogButtonSizer()
        okBt = wx.Button(self, wx.ID_OK, u'确定')
        okBt.SetDefault()
        self.Bind(wx.EVT_BUTTON, self.OnOk , okBt)
        cancelBt = wx.Button(self, wx.ID_CANCEL, u'取消')
        buttonSizer.AddButton(okBt)
        buttonSizer.AddButton(cancelBt)
        buttonSizer.Realize()
        
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(modeSizer, flag=wx.EXPAND|wx.ALL, proportion = 1,  border=5)
        sizer.Add(fontQSizer, flag=wx.EXPAND|wx.ALL, proportion = 0,  border=5)
        sizer.Add(typesetSizer, flag=wx.EXPAND|wx.ALL, proportion = 0,  border=5)
        sizer.Add(fontSizer,  flag=wx.EXPAND|wx.ALL, proportion = 0,  border=5)
        sizer.Add(buttonSizer, flag = wx.ALIGN_CENTER|wx.ALL, proportion = 0, border=5)
        self.SetSizer(sizer)
        sizer.Fit(self)
        
        self.getConfig()
    
    def getConfig(self):
        self.modeCh.SetSelection(self.transformConfig['mode'])
        self.OnChMode(None)
        self.fontQCh.SetSelection(self.transformConfig['fontQualifier'])
        self.OnChFontQ(None)
        self.autoTypesetCb.SetValue(self.transformConfig['autoTypeset'])
        self.lineLengthSpin.SetValue(self.transformConfig['lineLength'])
    
    def setConfig(self):
        self.transformConfig['mode'] = self.modeCh.GetSelection()
        self.transformConfig['autoTypeset'] = self.autoTypesetCb.GetValue()
        self.transformConfig['lineLength'] = self.lineLengthSpin.GetValue()
        self.transformConfig['fontQualifier'] = self.fontQCh.GetSelection()
        self.transformConfig.write()
        
        font = self.modeText.GetFont()
        self.fontConfig['face'] = font.GetFaceName().encode("utf-8")
        self.fontConfig['size'] = font.GetPointSize()
        self.fontConfig['style'] = font.GetStyle()
        self.fontConfig['weight'] = font.GetWeight()
        self.fontConfig.write()
    
    def OnOk(self, event):
        self.setConfig()
        self.EndModal(wx.ID_OK)
    
    def OnChMode(self, event):
        v = transform.trans_func.values()
        t = v[self.modeCh.GetSelection()]['example']
        self.modeText.SetValue(t)

    def OnChFontQ(self, event):
        v = transform.font_qualifier.values()
        sel = self.fontQCh.GetSelection()
        if sel == 0:
            self.fontQText.SetValue('')
        else:
            self.fontQText.SetValue('%s\n...\n%s' % (v[sel]['prefix'], v[sel]['suffix']))
    
    def OnSelectFont(self, event):
        data = wx.FontData()
        data.EnableEffects(True)
        data.SetInitialFont(self.modeText.GetFont())
        dlg = wx.FontDialog(self, data)
        if dlg.ShowModal() == wx.ID_OK:
            data = dlg.GetFontData()
            font = data.GetChosenFont()
            self.modeText.SetFont(font)
            self.fontBt.SetFont(font)
            self.fontBt.SetLabel('%s %d' % (font.GetFaceName(), font.GetPointSize()))
            self.fontBt.SetSize(self.fontBt.GetBestSize())

        dlg.Destroy()
